<?php
/**
 * Copy to a new Survey - Main Screen
 * 
 * @author	Xavier VILAIN
 * @package Nawa\Survey
 * @license #license-nawa-survey#
 * 
 */
use Nawa\Survey\Section;
use Fox\Html\Input;
use Nawa\MedicalStudy\MedicalCenter\Monitoring\Type;
use Nawa\Survey\Question;
use Nawa\Messages;
use Fox\Html\Block;
use Fox\Html\Button;
use Fox\Generator;
use Nawa\Survey;
use Fox\Html\Screen;

$xview = Xview::instance();
$survey = $xview->component('survey','object');
$data = $survey->getData();
$data['sections'] = $survey->tree();

//@TODO add authorization check here
$screen = new Screen('sSurver','bcopy');

//@TODO add breadcrumb
$screen->pageTitle(Msg(Nawa\Messages::BUNDLE,Nawa\Messages::SURVEY_COPY_TITLE,$survey->label()));
$screen->toolbar()
		->add(new Button('back',Generator::url('survey','update',null,array('surveyid' => $survey->getId()))));
// Header Block
$blHeader = new Block('bHeader',Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_HEADER_DATA));
$blHeader->add(new Input(array('survey_copy','copy_name'),true));
$screen->add($blHeader);
// Section/Question Block
$gdSections = $xview->module(Generator::url('survey','sections',null,
						array('surveyid' => $survey->getId())));
$gdSections->fieldcat()->field('iedit')->noOutput();
$gdSections->fieldcat()->field('add')->noOutput();
$gdSections->fieldcat()->field('idele')->noOutput();

$gdSections->setCoche();
$blSection = new Block('bSections',Msg(Nawa\Messages::BUNDLE,Nawa\Messages::SURVEY_SECTIONS));
$blSection->add($gdSections);
$blSection->message(Msg(Nawa\Messages::BUNDLE,Nawa\Messages::SURVEY_COPY_SECTIONS_MESSAGE));
$screen->add($blSection)->collapsed(false);

$screen->initialize($data);

if($screen->submitted()) {
	$fields = $screen->get();
	if(!$screen->errors()) {
		try {
			$newSurvey = new Survey();
			$newSurvey->setFields(array('label' => $fields['copy_name'],
										'language' => $data['language'],
										'category' => $data['category'],
										'type' => $data['type']));
			$empty = true;
			$tabix = 0;
			foreach($fields['sections'] as $sectionData) {
				if(!isset($sectionData['coche']) || !$sectionData['coche'])  {
					continue;
				}
				$empty = false;
				if(preg_match('/\:/',$sectionData['id'])) {
					$sectionData = $survey->findSections($sectionData['id'])->getData();
					unset($sectionData['surveyid']);
					$section = new Section();
					$section->setFields($sectionData);
					$newSurvey->addToSections($section);
				} else {
					$question = \Fox\Dao::load(Question::className(),$sectionData['id']);
					$questions[$tabix] = $question->getNonProtectedData();
					$oldQuestionId[$question->questionid] = $tabix;
					$tabix++;
				}
			}
			if($empty) {
				Fox\Message::Error(Msg(Nawa\Messages::BUNDLE,Nawa\Messages::SURVEY_COPY_EMPTY_ERROR));
			} else {
				$newSurvey->create();
				// Add Questions
				if(isset($questions)) {
					$newId = $newSurvey->getId();
					foreach($questions as $tabix => $questionData) {
						$question = new Question();
						$questionData['surveyid'] = $newId;
						if(!empty($questionData['parentid'])) {
							if(isset($oldQuestionId[$questionData['parentid']])) {
								$tabixIndex = $oldQuestionId[$questionData['parentid']];
								if(isset($newQuestionId[$tabixIndex])) {
									$questionData['parentid'] = $newQuestionId[$tabixIndex];
								}
							}
						}
						$question->setFields($questionData);
						$question->create();
						$newQuestionId[$tabix] = $question->questionid;
					}
				}
				// Add Survey To Available Monitoring list
				$type = new Type();
				$type->setFields(array('surveyid' => $newId,'active' => 1,'standard' => 0));
				$type->create();
				Fox\Message::Success(Msg(Nawa\Messages::BUNDLE,Nawa\Messages::SURVEY_HAS_BEEN_CREATED,$newSurvey->label()));
				$screen->redirect(Generator::url('survey','update',null,array('surveyid' => $newId)));
			}
 		} catch (Fox\Object\Exception\Save $e) {
			Fox\Message::Error($e);
		} catch (Fox\Object\Exception\NotExist $e) {
			Fox\Message::Error($e);
		}
	}
}

echo $screen->display();